package com.slipper.system.codeless.service;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataTableOrdinalDao;
import com.slipper.system.codeless.bo.DataTableOrdinalBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class DataTableOrdinalService {
    @Resource
    private DataTableOrdinalDao dataTableOrdinalDao;
    
    public DataTableOrdinalBO queryBy(String id) {
        return dataTableOrdinalDao.queryBy(id);
    }
    
    public List<DataTableOrdinalBO> queryList(DataTableOrdinalBO bo) {
        return dataTableOrdinalDao.queryList(bo);
    }
    
    public PageInfo<DataTableOrdinalBO> queryPage(DataTableOrdinalBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataTableOrdinalDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(String tableId, List<DataTableOrdinalBO> list, String employeeId) {
        if (Tool.isBlank(tableId)) {
            return 0;
        }

        // 保存/更新数据
        int i = 0;
        for (DataTableOrdinalBO ordinal : list) {
            ordinal.setTableId(tableId);
            ordinal.setOrdinal(++i);
            if (Tool.isBlank(ordinal.getId())) {
                ordinal.setId(UUID.getUUID());
                ordinal.setUpdateId(employeeId);
                ordinal.setCreateId(employeeId);
                ordinal.setUpdateTime(System.currentTimeMillis());
                ordinal.setCreateTime(System.currentTimeMillis());
                dataTableOrdinalDao.insert(ordinal);
            } else {
                ordinal.setUpdateId(employeeId);
                ordinal.setUpdateTime(System.currentTimeMillis());
                dataTableOrdinalDao.update(ordinal);
            }
        }

        // 删除被删除的数据
        DataTableOrdinalBO qo = new DataTableOrdinalBO();
        qo.setTableId(tableId);
        dataTableOrdinalDao.queryList(qo).stream()
                .filter(ordinal -> list.stream().noneMatch(temp -> ordinal.getId().equals(temp.getId())))
                .forEach(ordinal -> dataTableOrdinalDao.delete(ordinal));
        return 1;
    }

    @WriteTransactional
    public void saveList(List<DataTableOrdinalBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (dataTableOrdinalDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                dataTableOrdinalDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                dataTableOrdinalDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public void deleteByTableId(String tableId) {
        if (Tool.isBlank(tableId)) {
            return;
        }

        dataTableOrdinalDao.deleteByTableId(tableId);
    }

    @WriteTransactional
    public void copyByTableId(String oldTableId, String newTableId, String employeeId) {
        if (Tool.isBlank(oldTableId) || Tool.isBlank(newTableId)) {
            return;
        }

        DataTableOrdinalBO qo = new DataTableOrdinalBO();
        qo.setTableId(oldTableId);
        dataTableOrdinalDao.queryList(qo).forEach(ordinal -> {
            ordinal.setId(UUID.getUUID());
            ordinal.setTableId(newTableId);
            ordinal.setUpdateId(employeeId);
            ordinal.setCreateId(employeeId);
            ordinal.setUpdateTime(System.currentTimeMillis());
            ordinal.setCreateTime(System.currentTimeMillis());
            dataTableOrdinalDao.insert(ordinal);
        });
    }

    public List<DataTableOrdinalBO> queryByTableId(String tableId) {
        if (Tool.isBlank(tableId)) {
            return Collections.emptyList();
        }

        DataTableOrdinalBO qo = new DataTableOrdinalBO();
        qo.setTableId(tableId);
        return dataTableOrdinalDao.queryList(qo);
    }
}
